package com.moody.hibernate.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.LgdH;

/**
 * A data access object (DAO) providing persistence and search support for LgdH
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.moody.hibernate.domain.LgdH
 * @author MyEclipse Persistence Tools
 */

public class LgdHDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(LgdHDAO.class);
	// property constants
	public static final String DEBT_NO = "debtNo";
	public static final String DEBT_BALANCE = "debtBalance";
	public static final String EAD = "ead";
	public static final String LGD = "lgd";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";

	protected void initDao() {
		// do nothing
	}

	public void save(LgdH transientInstance) {
		log.debug("saving LgdH instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(LgdH persistentInstance) {
		log.debug("deleting LgdH instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public LgdH findById(java.lang.String id) {
		log.debug("getting LgdH instance with id: " + id);
		try {
			LgdH instance = (LgdH) getHibernateTemplate().get(
					"com.moody.hibernate.domain.LgdH", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(LgdH instance) {
		log.debug("finding LgdH instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding LgdH instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from LgdH as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByDebtNo(Object debtNo) {
		return findByProperty(DEBT_NO, debtNo);
	}

	public List findByDebtBalance(Object debtBalance) {
		return findByProperty(DEBT_BALANCE, debtBalance);
	}

	public List findByEad(Object ead) {
		return findByProperty(EAD, ead);
	}

	public List findByLgd(Object lgd) {
		return findByProperty(LGD, lgd);
	}

	public List findByInputUserId(Object inputUserId) {
		return findByProperty(INPUT_USER_ID, inputUserId);
	}

	public List findByInputOrgId(Object inputOrgId) {
		return findByProperty(INPUT_ORG_ID, inputOrgId);
	}

	public List findByInputDate(Object inputDate) {
		return findByProperty(INPUT_DATE, inputDate);
	}

	public List findAll() {
		log.debug("finding all LgdH instances");
		try {
			String queryString = "from LgdH";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public LgdH merge(LgdH detachedInstance) {
		log.debug("merging LgdH instance");
		try {
			LgdH result = (LgdH) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(LgdH instance) {
		log.debug("attaching dirty LgdH instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(LgdH instance) {
		log.debug("attaching clean LgdH instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static LgdHDAO getFromApplicationContext(ApplicationContext ctx) {
		return (LgdHDAO) ctx.getBean("LgdHDAO");
	}
}